package com.topboya.linker.ui.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.topboya.linker.R;
import com.topboya.linker.model.response.DictionaryResponse;
import com.topboya.linker.model.response.FeekbackReponse;
import com.topboya.linker.model.response.VisitTypeResponse;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;


public class HaspitalTypeAdapter extends RecyclerView.Adapter<HaspitalTypeAdapter.ListItemViewHolder> {

  private static final String TAG = "HaspitalTypeAdapter:";

    private List<DictionaryResponse.Data> mList;
    private Context mContext;
    //   用来保存被选中的对象
    private LinkedHashMap<String, DictionaryResponse.Data> mSelectedPositions = new LinkedHashMap<>();


    public HaspitalTypeAdapter(Context mContext, List<DictionaryResponse.Data> list) {

        this.mContext = mContext;
        mList = list;
    }


    //获得选中条目的结果
    public ArrayList<DictionaryResponse.Data> getSelectedItem() {

        ArrayList<DictionaryResponse.Data> selectList = new ArrayList<>();
        for (Map.Entry<String, DictionaryResponse.Data> entry : mSelectedPositions.entrySet()) {

            DictionaryResponse.Data productDetailInfoList = entry.getValue();
            selectList.add(productDetailInfoList);

        }
        return selectList;
    }


    @Override
    public ListItemViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {

        View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.visit_type_item, viewGroup, false);
        return new ListItemViewHolder(itemView);
    }

    //设置给定位置条目的选择状态
    public void setItemChecked(String id, DictionaryResponse.Data feekbackDataList) {
        mSelectedPositions.clear();
        mSelectedPositions.put(id, feekbackDataList);
    }

    //设置给定位置条目的选择状态
    public void setItemUnChecked(String id) {

        mSelectedPositions.remove(id);
    }


    //根据位置判断条目是否选中
    private DictionaryResponse.Data isItemChecked(int position) {

        return mSelectedPositions.get(mList.get(position).getId());
    }


    //绑定界面，设置监听
    @Override
    public void onBindViewHolder(final ListItemViewHolder holder, final int i) {

        holder.tv_productName.setText(mList.get(i).getLabel());
        if (null != isItemChecked(i)) {

            holder.tv_productName.setTextColor(Color.WHITE);
            holder.tv_productName.setBackgroundResource(R.drawable.select_oval_bg);
        } else {

            holder.tv_productName.setTextColor(mContext.getResources().getColor(R.color.select_visit_normal));
            holder.tv_productName.setBackgroundResource(R.drawable.no_select_visitype_bg);
        }

        //条目view的监听
        holder.itemView.setOnClickListener(v -> {

            if (null != isItemChecked(i)) {

                setItemUnChecked(mList.get(i).getId());

            } else {

                setItemChecked(mList.get(i).getId(),mList.get(i));
            }
            notifyDataSetChanged();
            if (null !=itemClickListener){

                itemClickListener.onClick(holder.itemView, i);
            }
        });

    }

    @Override
    public int getItemCount() {

        return mList == null ? 0 : mList.size();
    }

    public class ListItemViewHolder extends RecyclerView.ViewHolder {

        //ViewHolder
        TextView tv_productName;

        ListItemViewHolder(View view) {
            super(view);
            this.tv_productName = view.findViewById(R.id.tv_visittype);

        }

    }

    private OnItemClickListener itemClickListener;

    public void setOnItemClickListener(OnItemClickListener itemClickListener) {

        this.itemClickListener = itemClickListener;
    }

    public interface OnItemClickListener {

        void onClick(View v, int position);
    }
}


